//
//  MyDraggableImage.m
//  simple
//
//  Created by Echo on 13-12-25.
//  Copyright (c) 2013年 Echo. All rights reserved.
//

#import "MyDraggableImage.h"

@implementation MyDraggableImage
@synthesize delegate;
@synthesize name, _id, type;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    // Retrieve the touch point 检索接触点
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    if (self.tag == bg_tag) {
        self.tag = 102;
    }
//    [[self superview] bringSubviewToFront:self];
    CGRect frame = [self frame];
    beginFrame = frame;
    [delegate clickButtonBegin:frame];
}
-(void)setImage:(UIImage*)image{
    [self setBackgroundImage:image forState:UIControlStateNormal];
}
int fsdafds = 0;

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
    // Move relative to the original touch point 相对以前的触摸点进行移动
    
    CGPoint pt = [[touches anyObject] locationInView:self];
    
    CGRect frame = [self frame];
    
    frame.origin.x += pt.x - startLocation.x;
    
    frame.origin.y += pt.y - startLocation.y;
    
    fsdafds = frame.origin.x + frame.size.width;
    
    [delegate clickButtonMove:fsdafds view:self];
    
    [self setFrame:frame];
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.tag == 102) {
        self.tag = bg_tag;
        self.hidden = YES;
    }
    [delegate clickButtonEnd:[self frame] name:self.name view:self];
    if (self.tag == bg_tag) {
        [self setFrame:beginFrame];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
